<?php
ob_start();
if(isset($_SERVER['HTTP_APPVERSION'])){
include "./saesiteinfo.php";
}else{
 if(!file_exists("./install/install.lock")){
header('location:./install');
exit;
 }else{
include "./siteinfo.php";
 }
}
//连接数据库
$localhost=mysql_connect($sqlhost,$sqluser,$sqlpass)or die("无法连接服务器");
mysql_select_db($sqldata,$localhost);
if (isset($_COOKIE["SHANIUUSER"]) && isset($_COOKIE["SHANIUPASS"])){
$user=$_COOKIE["SHANIUUSER"];
$pass=$_COOKIE["SHANIUPASS"];
//用户数据
$mysql = mysql_query("SELECT * FROM user WHERE user='$user'");
$sql= mysql_fetch_array($mysql);
//查询QQ个数
$qqnumsql="SELECT COUNT(id) FROM qqlist WHERE myqq='$user' ";
$qqnumqqlist = mysql_fetch_array( mysql_query($qqnumsql) );
$qqnum=$qqnumqqlist[0];

if(isset($_pass['pass'])){
echo "您还没有登录";
}else{
?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="css/stylesheets.css" rel="stylesheet" type="text/css" />
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script src="js/qq.login.js"></script>
<script type="text/javascript">
var xiha={
    postData: function(url, parameter, callback, dataType, ajaxType) {
		if(!dataType) dataType='json';
		$.ajax({
			type: "POST",
			url: url,
			async: true,
			dataType: dataType,
			json: "callback",
			data: parameter,
			success: function(data) {
				if (callback == null) {
					return;
				} 
				callback(data);
			},
			error: function(error) {
				alert('创建连接失败');
			}
		});
	}
}
function login(uin,p,vcode,pt_verifysession){
	$('#load').html('正在登陆，请稍等...');
	var loginurl="login.php?do=qqlogin";
	xiha.postData(loginurl,"uin="+uin+"&p="+p+"&vcode="+vcode+"&pt_verifysession="+pt_verifysession+"&r="+Math.random(1), function(d) {
		if(d.saveOK ==0){
			$('#load').html('SID获取成功，请稍等...');
			save(d.uin,d.sid,d.skey);
		}else if(d.saveOK ==4){
			$('#load').html('验证码错误，重新生成验证码，请稍等...');
			getvc(d.uin);
		}else if(d.saveOK ==3){
			$('#load').html('您输入的帐号或密码不正确，请重新登录！');
			$('#submit').val('重新添加');
			$('.code').hide();
		}else{
			alert(d.msg);
		}
	});
	
}
function save(uin,sid,skey){
	var pwd=$('#pwd').val();
	window.location.href = 'qplus_do.php?qq='+uin+'&sid='+sid+'&skey='+skey+'&pwd='+pwd;
}
function getvc(uin,sig){
	$('#load').html('获取验证码，请稍等...');
	var getvcurl="login.php?do=getvc";
	xiha.postData(getvcurl,'uin='+uin+'&sig='+sig+'&r='+Math.random(1), function(d) {
		if(d.saveOK ==0){
			$('#load').html('请输入验证码');
			$('#codeimg').attr('vc',d.vc);
			$('#codeimg').html('<img onclick="getvc(\''+uin+'\',\''+d.vc+'\')" src="login.php?do=getvcpic&uin='+uin+'&sig='+d.vc+'&r='+Math.random(1)+'">');
			$('#submit').attr('do','code');
			$('.code').show();
		}else{
			alert(d.msg);
		}
	});

}
function dovc(uin,code,vc){
	$('#load').html('验证验证码，请稍等...');
	var getvcurl="login.php?do=dovc";
	xiha.postData(getvcurl,'uin='+uin+'&ans='+code+'&sig='+vc+'&r='+Math.random(1), function(d) {
		if(d.rcode ==0){
			var pwd=$('#pwd').val();
			p=getmd5(uin,pwd,d.randstr.toUpperCase());
			login(uin,p,d.randstr.toUpperCase(),d.sig);
			
		}else{
			$('#load').html('验证码错误，重新生成验证码，请稍等...');
			getvc(uin,vc);
		}
	});

}
$(document).ready(function(){
	$('#submit').click(function(){
		var self=$(this);
		$('#load').html('登录中，请稍候...');
		var uin=$('#uin').val(),
			pwd=$('#pwd').val();
		if(self.attr('do') == 'code'){
			var vcode=$('#code').val(),
				vc=$('#codeimg').attr('vc');
			dovc(uin,vcode,vc);
		}else if(self.attr('do') == 'update'){
			var sid=$('#sid').val(),
				skey=$('#skey').val();
			window.location.href="<?php echo $_GET['surl'];?>&uin="+uin+"&sid="+sid+"&skey="+skey;
		}else{
		if (self.attr("data-lock") === "true") return;
			else self.attr("data-lock", "true");
		var checkvcurl="login.php?do=checkvc";
		xiha.postData(checkvcurl,'uin='+uin+'&r='+Math.random(1), function(d) {
			if(d.saveOK ==0){
				var strs= new Array(); //定义一数组
				strs=d.data.split(",");
				if(strs[0]==0){
					pt_verifysession=strs[3];
					p=getmd5(uin,pwd,strs[2]);
					login(strs[1],p,strs[2],pt_verifysession);
				}else{
					getvc(uin,strs[2]);
				}
			}else{
				alert(d.msg);
				$('#load').html('');
			}
			self.attr("data-lock", "false");
		});
		}
	});
});
</script>

<?php
if ($qqnum>=$sql['peie']+$chushi){
echo "您的挂Q配额已满!请扩充。";
}else{
echo '
<form name="qq" action="./qplus_do.php" method="post">
        <div class="row-fluid">
            <div class="row-form">
                <div class="span12">
                    <input type="text" id="uin" placeholder="QQ账号"/>
                </div>
            </div>
            <div class="row-form">
                <div class="span12">
                    <input type="password" id="pwd" placeholder="QQ密码"/>
                </div>            
            </div>
			<div id="load" class="row-form" style="color:red;"></div>
			<div class="code" class="row-form" align="center" style="display: none;">
			<p id="codeimg">请输入验证码</p>
			<input type="text" class="form-control" id="code">
			</div>
            <div class="row-form">
                <div class="span12">
                    <span id="submit" class="btn">登录</span>
                </div>                
            </div>
        </div>
</form>

';
}

}

}else{
echo "您需要登陆后才能继续操作!";
}
?>